/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package cn.icanci.operation.controller;

import cn.hutool.core.collection.CollectionUtil;
import cn.icanci.basic.commonutils.R;
import cn.icanci.operation.entity.BaseTicketDataVO;
import cn.icanci.operation.entity.FlightNumberOption;
import cn.icanci.operation.entity.Ticket;
import cn.icanci.operation.entity.TicketDetailsVO;
import cn.icanci.operation.form.UserSearchForm;
import cn.icanci.operation.service.TicketService;
import cn.icanci.operation.service.UploadExcelService;
import cn.icanci.operation.utils.TradeLogService;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import org.apache.commons.compress.utils.Lists;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * 机票
 * @author icanci
 * @date Created in 2021/04/29 11:52
 * @version 1.0
 */
@RestController
@Slf4j
@RequestMapping("/operationAnalysis")
public class TicketController {

    @Resource
    private UploadExcelService uploadExcelService;
    @Resource
    private TicketService      ticketService;
    @Resource
    private TradeLogService    tradeLogService;

    @GetMapping("/queryTicketList")
    public R queryTicketList() {
        try {
            tradeLogService.setLogInfo("TICKET_CONTROLLER", "QUERY_TICKET_LIST", "");
            List<Ticket> ticketList = ticketService.queryManyByFlightNumber();
            if (CollectionUtil.isEmpty(ticketList)) {
                tradeLogService.warn(log, "返回结果为空，暂无机票数据");
                return R.error().message("返回结果为空，暂无机票数据");
            }
            tradeLogService.info(log, "加载成功～");
            return R.ok().message("加载成功").data("tickets", ticketList);
        } finally {
            tradeLogService.removeAll();
        }
    }

    @PostMapping("/userQueryTicket")
    public R userQueryTicket(@RequestBody UserSearchForm userSearchForm) {
        try {
            tradeLogService.setLogInfo("TICKET_CONTROLLER", "USER_QUERY_TICKET", "");
            List<Ticket> ticketList = ticketService.userQueryTicket(userSearchForm);
            if (CollectionUtil.isEmpty(ticketList)) {
                tradeLogService.warn(log, "返回结果为空，暂无机票数据");
                return R.error().message("返回结果为空，暂无机票数据");
            }
            tradeLogService.info(log, "查询成功～");
            return R.ok().message("查询成功").data("tickets", ticketList);
        } finally {
            tradeLogService.removeAll();
        }
    }

    @GetMapping("/queryBaseTicketData")
    public R queryBaseTicketData() {
        try {
            tradeLogService.setLogInfo("TICKET_CONTROLLER", "QUERY_BASE_TICKET_DATA", "");
            List<BaseTicketDataVO> ticketList = ticketService.querySellOut();
            if (CollectionUtil.isEmpty(ticketList)) {
                tradeLogService.warn(log, "返回结果为空，暂无机票数据");
                return R.error().message("返回结果为空，暂无机票数据");
            }
            tradeLogService.info(log, "加载成功～");
            return R.ok().message("加载成功").data("baseTicketData", ticketList);
        } finally {
            tradeLogService.removeAll();
        }
    }

    @GetMapping("/queryFlightNumberList")
    public R queryFlightNumberList() {
        try {
            tradeLogService.setLogInfo("TICKET_CONTROLLER", "QUERY_FLIGHT_NUMBER_LIST", "");
            List<String> flightNumberList = ticketService.queryFlightNumberManyRow();
            if (CollectionUtil.isEmpty(flightNumberList)) {
                tradeLogService.warn(log, "返回结果为空，暂无机票数据");
                return R.error().message("返回结果为空，暂无机票数据");
            }
            List<FlightNumberOption> flightNumberOptions = Lists.newArrayList();
            for (String s : flightNumberList) {
                FlightNumberOption flightNumberOption = new FlightNumberOption();
                flightNumberOption.setLabel(s);
                flightNumberOption.setValue(s);
                flightNumberOptions.add(flightNumberOption);
            }
            tradeLogService.info(log, "加载成功～");
            return R.ok().message("加载成功").data("flightNumberOptions", flightNumberOptions);
        } finally {
            tradeLogService.removeAll();
        }
    }

    @GetMapping("/queryAllByFlightNumber/{flightNumber}")
    public R queryAllByFlightNumber(@PathVariable("flightNumber") String flightNumber) {
        try {
            tradeLogService.setLogInfo("TICKET_CONTROLLER", "QUERY_ALL_BY_FLIGHT_NUMBER", "");
            List<Ticket> ticketList = ticketService.queryAllByFlightNumber(flightNumber);
            if (CollectionUtil.isEmpty(ticketList)) {
                tradeLogService.warn(log, "返回结果为空，暂无机票数据");
                return R.error().message("返回结果为空，暂无机票数据");
            }
            tradeLogService.info(log, "加载成功～");
            return R.ok().message("加载成功").data("tickets", ticketList);
        } finally {
            tradeLogService.removeAll();
        }
    }

    @GetMapping("/queryAllByFlightNumberNotSellout/{flightNumber}")
    public R queryAllByFlightNumberNotSellout(@PathVariable("flightNumber") String flightNumber) {
        try {
            tradeLogService.setLogInfo("TICKET_CONTROLLER", "QUERY_ALL_BY_FLIGHT_NUMBER_NOT_SELL_OUT", "");
            List<Ticket> ticketList = ticketService.queryAllByFlightNumber(flightNumber);
            if (CollectionUtil.isEmpty(ticketList)) {
                tradeLogService.warn(log, "返回结果为空，暂无机票数据");
                return R.error().message("返回结果为空，暂无机票数据");
            }
            tradeLogService.info(log, "加载成功～");
            List<Ticket> collect = ticketList.stream().filter(o -> o.getStatus() == 1).collect(Collectors.toList());
            return R.ok().message("加载成功").data("tickets", collect);
        } finally {
            tradeLogService.removeAll();
        }
    }

    @PostMapping("/uploadTicketExcel")
    public R uploadTicketExcel(@RequestParam("file") MultipartFile multipartFile) {
        return uploadExcelService.uploadTicket(multipartFile);
    }

    @GetMapping("/queryTicketDetailData/{flightNumber}")
    public R queryTicketDetailData(@PathVariable("flightNumber") String flightNumber) {
        try {
            tradeLogService.setLogInfo("TICKET_CONTROLLER", "QUERY_TICKET_DETAIL_DATA", "");
            List<TicketDetailsVO> ticketDetailData = ticketService.queryTicketDetailByFlightNumber(flightNumber);
            if (CollectionUtil.isEmpty(ticketDetailData)) {
                tradeLogService.warn(log, "返回结果为空，暂无机票数据");
                return R.error().message("返回结果为空，暂无机票数据");
            }
            tradeLogService.info(log, "加载成功～");
            return R.ok().message("加载成功").data("ticketDetailData", ticketDetailData);
        } finally {
            tradeLogService.removeAll();
        }
    }

    @GetMapping("/queryTicketDetailDataById/{id}")
    public R queryTicketDetailDataById(@PathVariable("id") Long id) {
        try {
            tradeLogService.setLogInfo("TICKET_CONTROLLER", "QUERY_TICKET_DETAIL_DATA", "");
            Ticket ticket = ticketService.queryTicketDetailDataById(id);
            if (null == ticket) {
                tradeLogService.warn(log, "返回结果为空，暂无机票数据");
                return R.error().message("返回结果为空，暂无机票数据");
            }
            tradeLogService.info(log, "加载成功～");
            return R.ok().message("加载成功").data("ticket", ticket);
        } finally {
            tradeLogService.removeAll();
        }
    }

    @GetMapping("/updateTicketDetailDataById/{id}")
    public R updateTicketDetailDataById(@PathVariable("id") Long id) {
        try {
            tradeLogService.setLogInfo("TICKET_CONTROLLER", "UPDATE_TICKET_DETAIL_DATA", "");
            ticketService.updateTicketDetailDataById(id);
            tradeLogService.info(log, "更新机票状态成功");
            return R.ok().message("加载成功");
        } finally {
            tradeLogService.removeAll();
        }
    }
}
